package com.nsyue.auth.config.security;


import com.nsyue.auth.constant.SysConstant;
import com.nsyue.auth.domain.provider.PlatformAuthenticationProvider;
import com.nsyue.auth.domain.provider.NsyueAuthenticationProvider;
import com.nsyue.auth.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * SpringSecurity的配置
 *
 * @author laixm
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 设置 HTTP 验证规则
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 允许表单登录
        http.authorizeRequests()
                .antMatchers(SysConstant.KNIFE4J_URLS).permitAll()
                .antMatchers("/oauth/**", "/login/**", "/sso/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/login")// 自定义登录页面
                // .loginProcessingUrl("/login")
                .permitAll()
                .and()
                .logout()
                // .logoutUrl("/logout")
                // .logoutSuccessUrl("/login")
                .permitAll()
                .and()
                .csrf()
                .disable();
    }



    /**
     * 该方法是登录的时候会进入
     *
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        PlatformAuthenticationProvider platformAuthenticationProvider = new PlatformAuthenticationProvider();
        platformAuthenticationProvider.setUserService(userService);

        auth.authenticationProvider(platformAuthenticationProvider);
        // 使用自定义身份验证组件
        auth.authenticationProvider(
                new NsyueAuthenticationProvider(
                        userService,
                        passwordEncoder,
                        userRoleService,
                        roleService,
                        rolePermissionService,
                        permissionService));
    }

    /**
     * BCrypt  加密
     *
     * @return PasswordEncoder
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
